$(function() {
  let userInfo = getCookie('user') || '',
    type = userInfo.type,
    openid = userInfo.openid,
    userv1info = getCookie('userv1info') || '',
    userbindinfo = getCookie('userbindinfo') || '',
    path = location.pathname,
    index = path.lastIndexOf('\/'),
    modifyflag = path.substring(index + 1, path.length);
  // 通过路由参数来判断用户当前处于哪个页面，然后将内容展示到页面上
  const user = '/user/v1/';
  const api = 'http://api.zymk.cn/app_api/v3/';
  let acMobile = $('.mobile');
  let acUserName = $('.acuser');
  let acEmail = $('.email');
  let acuser = $('.acuser');
  let vcObj = {
    vcPending: false, // 验证码是否已请求
    sec: 60
  }
  if(modifyflag === 'tel') {
    if(!userbindinfo.mkxq) {
      acMobile.hide();
    } else {
      userv1info.user.mobile ? acMobile.text('当前手机号：' + userv1info.user.mobile) : acMobile.text('大人～您还没有绑定手机');
    }
    telVcBtn();
    $('.updateBtn').on('click', function() {
      let mobile = $('#mobile').val();
      let vcode = $('.vcode').val();
      if(userbindinfo.mkxq) {
        // go modify api
        if(mobileReg(mobile) && vcode) {
          let uid = userv1info.user.uid;
          modifyMobile(mobile, vcode, uid);
        } else {
          console.log('qingshuruneiron')
        }
      } else {
        // go bind api
        bindFn(mobile, vcode, 'mobile');
        console.log('go bind api')
      }
    })
  } else if(modifyflag === 'mkxq') {
    acuser.text('当前账号：' + userv1info.user.username);

  } else if(modifyflag === 'email') {
    if(!userbindinfo.mkxq) {
      acEmail.hide();
    } else {
      userv1info.user.email ? acEmail.text('当前邮箱：' + userv1info.user.email) : acMobile.text('大人～您还没有绑定邮箱');
    }
    mailVcBtn();
    $('.updateBtn').on('click', function() {
      let email = $('#email').val();
      let vcode = $('.vcode').val();
      if(userbindinfo.mkxq) {
        // go modify api
        if(emailReg(email) && vcode) {
          let uid = userv1info.user.uid;
          modifyEmail(email, vcode, uid);
        }
      } else {
        // go bind api
        bindFn(email, vcode, 'email');
        console.log('go bind api')
      }
    })
  }

  function telVcBtn() {
    $('.getVc').on('click', function() {
      let mobile = $('#mobile').val();
      if(!vcObj.vcPending) {
        if(mobileReg(mobile)) {
          getVc(mobile, 'mobile')
        } else {
          console.log('请输入正确的手机号')
        }
      } else {
        console.log('请不要重复获取验证码')
      }
    })
  }

  function mailVcBtn() {
    $('.getVc').on('click', function() {
      let email = $('#email').val();
      if(!vcObj.vcPending) {
        if(emailReg(email)) {
          getVc(email, 'email')
        } else {
          console.log('请输入正确的邮箱号码')
        }
      } else {
        console.log('请不要重复获取验证码')
      }
    })
  }

  function getVc(value, type) {
    let data = {
      service: 'mkn',
    }
    data[type] = value;
    $.ajax({
      url: type === 'mobile' ? user + 'mobilevc/' : user + 'mailvc/',
      type: 'POST',
      data: data,
      success: function(res) {
        res = JSON.parse(res)
        if(res.status === 0) {
          vcObj.vcPending = true;
          var timer = setInterval(function() {
            vcObj.sec--;
            $('.getVc').text('倒计时' + vcObj.sec + '秒');
            if(vcObj.sec <= 0) {
              clearInterval(timer);
              $('.getVc').text('获取验证码');
              vcObj.sec = 60;
              vcObj.vcPending = false;
            }
          }, 1000);
        } else {
          console.log(res.msg)
        }
      }
    })
  }

  function modifyMobile(mobile, vcode, uid) {
    $.ajax({
      url: user + 'mobilechange/',
      type: 'POST',
      data: {
        mobile: mobile,
        vcode: vcode,
        uid: uid
      },
      success: function(res) {
        res = JSON.parse(res);
        if(res.status === 0) {
          console.log('绑定成功')
          userv1info.user.mobile = mobile;
          setCookie('userv1info', JSON.stringify(userv1info), 365);
        } else {
          console.log(res.msg)
        }
      }
    })
  }

  function modifyEmail(email, vcode, uid) {
    $.ajax({
      url: user + 'mailchange/',
      type: 'POST',
      data: {
        email: email,
        vcode: vcode,
        uid: uid
      },
      success: function(res) {
        res = JSON.parse(res);
        if(res.status === 0) {
          console.log('绑定成功')
          userv1info.user.email = mobile;
          setCookie('userv1info', JSON.stringify(userv1info), 365);
        } else {
          console.log(res.msg)
        }
      }
    })
  }

  function bindFn(value, vcode, type) {
    let data = { vcode };
    data[type] = value;
    $.ajax({
      url: type === 'mobile' ? user + 'mobilebind/' : user + "mailbind/",
      type: 'POST',
      data: data,
      success: function(res) {
        res = JSON.parse(res)
        console.log('bindfn', res)
        if(res.status === 0) {
          bindUser(userInfo.type, openid, 'mkxq', res.data.appToken);
        }
      }
    })
  }

  function bindUser(type, openid, target_type, target_token, type_tp) {
    $.ajax({
      url: api + 'binduser/?type=' + type + '&openid=' + openid + '&target_type=' + target_type + '&target_token=' + target_token + '&client=pc',
      dataType: 'jsonp',
      jsonp: 'callback',
      success: function(res) {
        if(res.status === 0) {
          setCookie('userbindinfo', '', -1);
          console.log(res.msg);
          setTimeout(() => {
            location.href = location.origin + '/personalset/bindAccount';
          }, 1500)
        } else {
          console.log(res.msg);
        }
      }
    })
  }
})